package com.syygl.cfTest;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureTest {

    public void testCompletableFuture() {
        //对completableFuture的理解
        //1、默认使用的是  ForkJoinPool.commonPool()
        //设置默认线程池的并发数
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "10");
        CompletableFuture.supplyAsync(() -> {return "123";});
        //CompletableFuture也可以使用指定线程池

    }




    TestService testService = new TestService();

    public void test() throws ExecutionException, InterruptedException {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return testService.getName();
        });
        CompletableFuture<String> stringCompletableFuture2 = CompletableFuture.supplyAsync(() -> {
            return testService.getName();
        });

        CompletableFuture[] array = new CompletableFuture[2];
        array[0] = stringCompletableFuture;
        array[1] = stringCompletableFuture2;

        CompletableFuture.allOf(array);//等待数组中的全部执行完成

        System.out.println("get" + array[0].get());

    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("CF123");
            return "S";
        });

        System.out.println("123");

        CompletableFutureTest completableFutureTest = new CompletableFutureTest();

        completableFutureTest.test();
    }



}
